<template>
    <div class="center-recipes">
        <div class="cr-list">
            <div class="cr-item" v-for="data in list" :key="data.menuId">
                <router-link :to="{
                  name: 'DishDetails',
                  params: {
                    id: data.menuId
                  }
                }">
                <img :src="data.menuSrc?data.menuSrc:'../../../assets/img/errorImg.png'" :onerror="errorImage" alt="">
                <p>{{data.menuName}}</p>
                </router-link>
                <span class="el-icon-view"></span>
                <span>{{Nums(data.doh)}}</span>
                <span class="el-icon-star-off"></span>
                <span>{{Nums(data.collection)}}</span>
                <i v-if="delFlag" class="el-icon-delete foodDelBtn" @click="delFoodFun(data)"></i>
            </div>
        </div>
        <el-empty :image-size="200" v-if="list.length === 0"></el-empty>
      <div class="page">
        <el-pagination
          class="pages"
          background
          layout="prev, pager, next"
          :total="pages.total"
          :hide-on-single-page=true
          :current-page="pages.current"
          :pager-count=5
          :page-size=12
          @current-change="handleCurrentChange">
        </el-pagination>
      </div>
    </div>
</template>
<script>
// import cookie from '../../../cookien'
export default {
  name: 'center-follower',
  data () {
    return {
      list: [],
      current_page: 1,
      total: 100,
      loading: true,
      // 分页
      pages: {
        // 每页十条，共几条
        total: 0,
        // 当前页数
        vals: 1,
        // 自定义第几页
        current: 1
      },
      errorImage: 'this.src="' + require('../../../assets/img/errorImg.png') + '"',
      delFlag: false
    }
  },
  computed: {
    Nums (value) {
      return (value) => {
        if (value >= 100000000) {
          value = Math.round(value / 10000000) / 10 + '亿'
        } else if (value >= 10000000) {
          value = Math.round(value / 1000000) / 10 + 'kw'
        } else if (value >= 10000) {
          value = Math.round(value / 1000) / 10 + 'w'
        } else if (value >= 1000) {
          value = Math.round(value / 100) / 10 + 'k'
        }
        return value
      }
    }
  },
  mounted () {
    this.getData()
    console.log()
    if (this.$route.params.userId === this.$store.state.userMsg.userId) {
      this.delFlag = true
    } else {
      this.delFlag = false
    }
  },
  methods: {
    delFoodFun (key) {
      this.$confirm('是否删除该菜谱？（注意：删除后将不可找回）', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios({
          method: 'delete',
          url: '/Menu/User/deleteMenu',
          params: {
            menuId: key.menuId,
            userId: key.userId
          }
        }).then(data => {
          console.log(data.data)
          if (data.data.code === 200) {
            this.$message({
              type: 'success',
              message: '已删除!'
            })
            this.getData()
          } else {
            this.$message({
              type: 'info',
              message: data.message
            })
          }
        }).catch(error => {
          console.log(error)
        })
      })
    },
    getData () {
      this.$axios({
        url: '/Menu/Visitor/findMenuByUserId',
        params: {
          userId: this.$route.params.userId,
          pageNow: this.pages.vals,
          pageSize: 12
        }
      }).then(data => {
        if (data.data.code === 200) {
          this.pages.total = data.data.object.total
          this.list = data.data.object.list
          this.loading = false
          //   this.userInfo = data.data.object
        }
      }).catch(error => {
        console.log(error)
      })
    },
    handleCurrentChange (val) {
      this.pages.vals = val
      this.getData()
    }
  }
}
</script>

<style lang="less" scoped>
.center-recipes{
    margin-top:10px;
    .cr-list{
        display: grid;
        grid-template-columns: repeat(3,306px);
        place-content: space-around space-between;
        .cr-item{
            margin-top:20px;
            img{
                width: 100%;
                height: 182px;
                border-radius: 8px;
                object-fit: cover;
            }
            p{
                color: #333;
                font-size: 15px;height:35px;
                line-height: 35px;
                text-overflow: ellipsis;
                overflow: hidden;white-space: nowrap;
                &:hover {
                    cursor: pointer;
                    color: #FFB31A;
                }
            }
            span{
                font-size: 14px;
                height:14px;
                line-height: 14px;
                margin:0 4px;
                display: inline-block;
            }
            .el-icon-view{
                transform:translateY(4px)
            }
        }
    }
}
</style>
